package vn.edu.saigontech.ncms.web.study;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.OfferingMember;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.model.result.CambridgeTest;
import vn.edu.saigontech.ncms.service.CambridgeTestService;
import vn.edu.saigontech.ncms.service.OfferingMemberService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.util.CollectionUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class CambridgeTestAction extends BaseAction {

	private Long id;
	private Long receiptId;
	private Long offeringId;

	private Long[] ids;
	private Double[] listenings;
	private Double[] speakings;
	private Double[] grammars;
	private Double[] readings;
	private Double[] writings;
	private String[] comments;

	// Make-up Test
	private Long finalTestId;
	private String date;
	private Double writtenScore;
	private Double speakingScore;
	private Double listeningScore;
	private Double grammarScore;
	private Double readingScore;
	private String comment;
	private String receiptNo;
	private Double fee;
	private Long cashierId;

	public String list() {
		REQUEST().setAttribute("offering",
				OfferingService.INSTANCE.findById(this.offeringId));
		REQUEST().setAttribute(
				"offeringMembers",
				OfferingMemberService.INSTANCE
						.findByOfferingId(this.offeringId));
		return SUCCESS;
	}

	public String edit() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		for (int i = 0; i < this.ids.length; i++) {
			CambridgeTest cambridgeTest = CambridgeTestService.INSTANCE
					.findById(this.ids[i]);
			offering.getCambridgeGradeDetail().injectGrade(cambridgeTest,
					listenings, speakings, grammars, readings, writings, null,
					i);
			cambridgeTest.setComment(this.comments[i]);
			CambridgeTestService.INSTANCE.persist(cambridgeTest);
		}
		this.addLoggingDetail(BaseAction.EDIT, "logging.cambridge.edit",
				offering.getName());
		return SUCCESS;
	}

	public String showReport() {
		Collection<OfferingMember> members = OfferingMemberService.INSTANCE
				.findByOfferingId(this.offeringId, true);
		boolean noted = false;
		Iterator<OfferingMember> iterator = members.iterator();
		while (iterator.hasNext() && !noted) {
			String comment = iterator.next().getMidtermTest().getComment();
			if (comment != null)
				if (comment.length() > 10) {
					noted = true;
				}
		}
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("noted", noted);
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("offeringMembers", members);

		this.addLoggingDetail(BaseAction.REPORT, "logging.cambridge.report1",
				offering.getName());
		return SUCCESS;
	}

	public String printTeachers(Collection<Teacher> teachers) {
		List<String> result = new ArrayList<String>();
		for (Teacher teacher : teachers) {
			result.add(this.i18n(teacher.getTitle().getKey()) + " "
					+ teacher.getFirstName());
		}
		return CollectionUtil.toString(result, " & ");
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public Long getReceiptId() {
		return receiptId;
	}

	public void setReceiptId(Long receiptId) {
		this.receiptId = receiptId;
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Double[] getListenings() {
		return listenings;
	}

	public void setListenings(Double[] listenings) {
		this.listenings = listenings;
	}

	public Double[] getSpeakings() {
		return speakings;
	}

	public void setSpeakings(Double[] speakings) {
		this.speakings = speakings;
	}

	public Double[] getReadings() {
		return readings;
	}

	public void setReadings(Double[] readings) {
		this.readings = readings;
	}

	public Double[] getWritings() {
		return writings;
	}

	public void setWritings(Double[] writings) {
		this.writings = writings;
	}

	public String[] getComments() {
		return comments;
	}

	public void setComments(String[] comments) {
		this.comments = comments;
	}

	public Double[] getGrammars() {
		return grammars;
	}

	public void setGrammars(Double[] grammars) {
		this.grammars = grammars;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public Double getWrittenScore() {
		return writtenScore;
	}

	public void setWrittenScore(Double writtenScore) {
		this.writtenScore = writtenScore;
	}

	public Double getSpeakingScore() {
		return speakingScore;
	}

	public void setSpeakingScore(Double speakingScore) {
		this.speakingScore = speakingScore;
	}

	public Double getListeningScore() {
		return listeningScore;
	}

	public void setListeningScore(Double listeningScore) {
		this.listeningScore = listeningScore;
	}

	public Double getGrammarScore() {
		return grammarScore;
	}

	public void setGrammarScore(Double grammarScore) {
		this.grammarScore = grammarScore;
	}

	public Double getReadingScore() {
		return readingScore;
	}

	public void setReadingScore(Double readingScore) {
		this.readingScore = readingScore;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getReceiptNo() {
		return receiptNo;
	}

	public void setReceiptNo(String receiptNo) {
		this.receiptNo = receiptNo;
	}

	public Double getFee() {
		return fee;
	}

	public void setFee(Double fee) {
		this.fee = fee;
	}

	public Long getCashierId() {
		return cashierId;
	}

	public void setCashierId(Long cashierId) {
		this.cashierId = cashierId;
	}

	public Long getFinalTestId() {
		return finalTestId;
	}

	public void setFinalTestId(Long finalTestId) {
		this.finalTestId = finalTestId;
	}
}
